package org.thunms.copyright.version.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thunms.copyright.version.dao.CopyrightValidateDao;
import org.thunms.copyright.version.entity.CopyrightValidateEntity;
import org.thunms.framework.license.LicenseFile;
import org.thunms.framework.license.Signaturer;
import org.thunms.framework.model.DataGrid;
import org.thunms.framework.model.DataGridJson;
/**
 * 版权管理平台
 * 版本注册子系统
 * 版本验证管理
 */
@Service
public class CopyrightValidateService  {
	private static final Logger logger=LoggerFactory.getLogger(CopyrightValidateService.class);
	@Autowired
	private CopyrightValidateDao copyrightValidateDao;

	
	@Transactional(readOnly=false)
	public String remoteValidate(String validate, String remoteAddr) {
		CopyrightValidateEntity entity=new CopyrightValidateEntity();
		entity.setId(UUID.randomUUID().toString());
		entity.setContent(validate);
		entity.setName(remoteAddr);
		entity.setCreateTime(new Date());
		this.copyrightValidateDao.save(entity);
		try {
			byte[] sign=Signaturer.sign("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCTTYYCMJCr93uQftqka+bcPbwsZGJJoI0vpSd190ULd0UCse1ez2WSh2JFTH6/HcV1vmvCHB0Laf7psGn0YzRhx+g9xNV1yWCC47ryiDni4vbkC1QcLbuTahhkFszArtXYIPWlTJ8qHPV/kC66xYYErMSPOJrfq40puXv7b1FRbOlRLA8H8E1+DVjvt9NVI2dEHY7cD2OLYYRoV5B/ZBJ+gScmWyNzX9ZDFWChpcn/i85lqOnNgOMt7FTBFlN9p252nxmV+3bvz1vqxgbSi3T1JmgGyY/POkNkNrzTawQQjjq5PmNaoq3o787Io+tlNQf9rw0jznJyotwY06MgAHkNAgMBAAECggEARz9hCSAaacM7lgl1kb/LG04U/VfUno40nA4DBzEByWj4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dpcufj8cwSTUrhXrXtAhmIsC0f3y7lDwThbY/mvWwFnmeQr07xzUx9hjYok5gL0Ih/93uKWTUXFXFKl/6KqZOCn/EVyAA7h99Fm".getBytes(), new String(LicenseFile.decode(validate)));
			return new String(sign);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return remoteAddr;
		}
		
		
	}
	public String downloadLicense(String id) {
		if(StringUtils.isNotBlank(id)){
			CopyrightValidateEntity entity=this.copyrightValidateDao.get(id);
			if(entity!=null){
				try {
					byte[] sign = Signaturer.sign("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCTTYYCMJCr93uQftqka+bcPbwsZGJJoI0vpSd190ULd0UCse1ez2WSh2JFTH6/HcV1vmvCHB0Laf7psGn0YzRhx+g9xNV1yWCC47ryiDni4vbkC1QcLbuTahhkFszArtXYIPWlTJ8qHPV/kC66xYYErMSPOJrfq40puXv7b1FRbOlRLA8H8E1+DVjvt9NVI2dEHY7cD2OLYYRoV5B/ZBJ+gScmWyNzX9ZDFWChpcn/i85lqOnNgOMt7FTBFlN9p252nxmV+3bvz1vqxgbSi3T1JmgGyY/POkNkNrzTawQQjjq5PmNaoq3o787Io+tlNQf9rw0jznJyotwY06MgAHkNAgMBAAECggEARz9hCSAaacM7lgl1kb/LG04U/VfUno40nA4DBzEByWj4DTSofopMM9Kd+qzkRWaOAsSG9I2KkuHC9Z9psFZnmMALmgzhvqYXs1eZLgQsE4z6JBVCqpqINuZDQ+4vPSeTsYVHq5tjBQiaygegQqCVkR9Lt4aGipc8oSjhSh1utLX5TVd7S2l5jBatft19njs2oubPb2ujUxFIpsBm3aurKqJYP36sQcPXIEMUximQPPSA7zFXoevvZfAbRKNEeitsiqhmm3cKWZYmaAYqF86vOD/Am8iwGwqpRHeNkIvu/m+tqaFCeKVfYqZnRhZ5Hl+U3nW/fzj8RpCXjy6OEmQHtQKBgQD+72ZI49foSDPp5t+xMMOYybvJRo0W9pUDyt68e416vQ/MczVOA0vDJQpHgT7i4bAEwIlxqg/rmmCdoTPEOXQoOyzuDD9F/HytnBQfapU3X9daWZSqb2A9LYgUuzCGV5Us3uSHU0Db8GnBTHFkbcoChNplnxNu/TDHkmOHzAAFdwKBgQCT6wiMi0ikOPkSFXmmFNRRzvdJmZY1XX9vjOIupqU3uM4yUC1vay8ANfA+yniR3NEapC0BinAC9NdFu2Q10lJo1kly7C0Hs5NJ5cR0FvISuYFogKxhet4dxSjtLPaqL5Iwk7xDqQ5c6NVwpVzT06C3bPAYys1AypE+i9Tsz56mmwKBgQDZ/gwQuAOfdF4v3GPbOD+JHrec3N84i5HQtGDR+zR3h8NCqCEknFzkeX/YtQP5LdM9vQXKVAlZBaEoKPgDCr3wyOkahQ514fka2yO52YHO3/1eHMurEaHRrmV2lLphpfLBc7cmoGINr/4+dAdiQR9EGCZIT0Zm2cokXWEOdbpu8QKBgHo5+oR4C3x3/4gr6quMBq3bAYf1u6M5MTZyCexWk6tpsCjvC7xHqzzXVT/tepe9CaSkCPR++nPW8ZOTdwjqVGKAv4yRD9yPTKQBksYCt+E1HHPH76lVJ/kdk+X4A5M5X+l4Clc2eMHZO5WrbvsyolkvUW6LFfP17vLqBPXx0b0FAoGBAPqjf1iTTlUdH+Knwqy03lNHTSKbFZTbWjmSWbAZt3aD6n9si/lHtM7g416e9xCc3GVzdUKl/dpcufj8cwSTUrhXrXtAhmIsC0f3y7lDwThbY/mvWwFnmeQr07xzUx9hjYok5gL0Ih/93uKWTUXFXFKl/6KqZOCn/EVyAA7h99Fm".getBytes(), new String(LicenseFile.decode(entity.getContent())));
					return new String(sign);
				} catch (UnsupportedEncodingException e) {
					logger.error("加密错误",e);
					return id;
				}
				
			}
			
		}
		
		return id;
	}
	public DataGridJson<CopyrightValidateEntity> getDataGridList(DataGrid dg, CopyrightValidateEntity mode) {
		DataGridJson<CopyrightValidateEntity> dgj=new DataGridJson<CopyrightValidateEntity>();
		List<Object> param=new ArrayList<Object>();
		StringBuilder hql=new StringBuilder(" from CopyrightValidateEntity where 1=1");
		dgj.setTotal(this.copyrightValidateDao.count(hql.toString(), param));
		dgj.setRows(this.copyrightValidateDao.query(hql.toString(), dg.getPage(), dg.getRows(), param));
		
		return dgj;
	}
	
	
}
